<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="" prop="id">
				<el-input v-model="dataForm.id" placeholder="" v-if="false"></el-input>
			</el-form-item>

			<el-row>
				<el-col :span="12">
					<el-form-item label="组织名称" prop="name">
						<el-input v-model="dataForm.name" placeholder=""></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="组织代码" prop="code">
						<el-input v-model="dataForm.code" placeholder=""></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="组织类型" prop="typeId">
						<el-tree-select v-model="dataForm.typeId" :data="typeList" value-key="id" check-strictly
							:render-after-expand="false" :props="{ label: 'name', children: 'children' }"
							style="width: 100%" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="上级组织" prop="superiorId">
						<el-tree-select v-model="dataForm.superiorId" :data="abbreviationList" value-key="id" check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'abbreviation', children: 'children' }" style="width: 100%" />
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="组织负责人" prop="superiorPrincipal">
						<el-input v-model="dataForm.superiorPrincipal" placeholder=""></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="组织简称" prop="abbreviation">
						<el-input v-model="dataForm.abbreviation" placeholder=""></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="工作地点" prop="workplaceId">
						<el-tree-select v-model="dataForm.workplaceId" :data="workplaceList" value-key="id" check-strictly
							:render-after-expand="false" :props="{ value: 'id', label: 'workplace', children: 'children' }"
							style="width: 100%" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="工作城市" prop="workplaceStr">
						<el-input v-model="dataForm.workplaceStr" placeholder=""></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="编制人数" prop="population">
						<el-input v-model="dataForm.population" placeholder=""></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="组织状态" prop="isEnable">
						<el-switch v-model="dataForm.isEnable" :active-value="1" :inactive-value="0" active-text="启用"
							inline-prompt inactive-text="停用" placeholder="" />
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="组织描述" prop="description">
				<el-input v-model="dataForm.description" placeholder=""></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useOrganizationApi, useOrganizationSubmitApi, findListAll } from '@/api/nb/organization/organization'
import { findAllApi } from '@/api/nb/organization/workplace'

const emit = defineEmits(['refreshDataList'])
defineProps(['typeList'])

const workplaceList = ref([])
const abbreviationList = ref([])
const visible = ref(false)
const dataFormRef = ref()
const pid = ref()

const dataForm = reactive({
	id: '',
	name: '',

	code: '',
	typeId: '',
	superiorId: '',
	superiorPrincipal: '',
	abbreviation: '',
	workplaceId: '',
	workplaceStr: '',
	population: '',
	isEnable: '',
	description: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getOrganization(id)
	} else {
		dataForm.superiorId = pid.value
		Object.assign(dataForm)
	}

	getWorkplace()
	getAbbreviation()
}

// 获取 工作地点
const getWorkplace = () => {
	return findAllApi().then(res => {
		workplaceList.value = res.data
	})
}

// 获取 上级组织
const getAbbreviation = () => {
	return findListAll().then(res => {
		abbreviationList.value = res.data
	})
}

const getOrganization = (id: number) => {
	useOrganizationApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useOrganizationSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init,
	pid
})
</script>
